package controllers

import(
	"beego1/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/httplib"
	"time"
	"fmt"
)

type CrawlMovieController struct {
	beego.Controller
}

func (c *CrawlMovieController) CrawlMovie() {
	var movieInfo models.MovieInfo
	// 链接到redis
	models.ConnectRedis("127.0.0.1:6379")

	// 爬虫入口url
	sUrl := "https://movie.douban.com/subject/26683290/"
	models.PutinQueue(sUrl)
	
	for {
		length := models.GetQueueLength()
		if length == 0 {
			break // 如果url队列为空 则退出当前循环
		}
		sUrl = models.PopfromQueue()
		if models.IsVisit(sUrl) {
			continue
		}
		rsp := httplib.Get(sUrl)
		sMovieHtml, err := rsp.String()
		if err != nil {
			panic(err)
		}
		movieInfo.Movie_name = models.GetMovieName(sMovieHtml)
		// 记录电影信息
		if movieInfo.Movie_name != "" {
			movieInfo.Movie_pic = models.GetMoviePic(sMovieHtml)
			movieInfo.Movie_director = models.GetMovieDirector(sMovieHtml)
			movieInfo.Movie_main_character = models.GetMovieMainCharacters(sMovieHtml)
			movieInfo.Movie_span = models.GetMovieTime(sMovieHtml)
			movieInfo.Movie_type = models.GetMovieGenre(sMovieHtml)
			movieInfo.Movie_grade = models.GetMovieGrade(sMovieHtml)
			movieInfo.Movie_on_time = models.GetMovieOnTime(sMovieHtml)
			id, err := models.AddMovie(&movieInfo)
			if err != nil{
				fmt.Println("err:", err)
			}
			fmt.Println(id)
		}
		// 提取该页面的所有电影连接
		urls := models.GetMovieUrls(sMovieHtml)
		for _,url := range urls {
			models.PutinQueue(url)
			c.Ctx.WriteString("<br>" + url + "</br>")
		}

		// surl 应当记录到 访问set中
		models.AddToSet(sUrl)
		time.Sleep(time.Second)
	}
	c.Ctx.WriteString("11")
}
